//
//  QualityScanViewController.m
//  MyProject
//
//  Created by Pei on 16/6/29.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "QualityScanViewController.h"
#import "SSCheckBoxView.h"
#import "UIImageView+WebCache.h"
#import "YMCaptureViewController.h"
#import "RequestPostUploadHelper.h"
#import "SelectCompanyAndProjectViewController.h"
#import "ShiGongStatusViewController.h"
#import "SearchGuanJianGongXuViewController.h"

#import "TZImagePickerController.h"
#import "UIView+Layout.h"
#import "TZTestCell.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
#import "LxGridViewFlowLayout.h"
#import "TZImageManager.h"
#import "TZVideoPlayerController.h"

@interface QualityScanViewController ()<UIImagePickerControllerDelegate,PassImageDelegate, UIActionSheetDelegate,UINavigationControllerDelegate, ShiGongStatusViewControllerDelegate, SearchGuanJianGongXuViewControllerDelegate, SelectCompanyAndProjectViewControllerDelegate,UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate,  TZImagePickerControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate>
{
    UIScrollView *_mainScrollView;
    
    //SSCheckBoxView *_currentBox;
    
    //SSCheckBoxView *_boxView;
    //SSCheckBoxView *_boxView1;
    
     NSString *TMP_UPLOAD_IMG_PATH;
    
    UIView *_headerView;
    UIView *_checkView;
    
    SSCheckBoxView *_boxView;
    SSCheckBoxView *_boxView1;
    SSCheckBoxView *_boxView2;
    
    SSCheckBoxView *_currentBox;
    
    
    UIView *_inputView;

    NSDictionary *companyDic;
    NSDictionary *projectDic;
    
    UILabel *lbl0;
    UILabel *lbl1;
    
    NSDictionary *currentDic;
    
    //UITextView *inputField;
    UITextField *inputField;
    
    NSMutableArray *_imageArray;
    NSMutableArray *_imageDataArray;
    
    int status;
    
    UIButton *addBtn;
    
    UILabel *lbl02;
    
    UITableView *_myTableView;
    
    UIControl *control02;
    
    
    NSMutableArray *_selectedPhotos;
    NSMutableArray *_selectedAssets;
    BOOL _isSelectOriginalPhoto;
    
    CGFloat _itemWH;
    CGFloat _margin;
    LxGridViewFlowLayout *_layout;
}
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (nonatomic, strong) UICollectionView *collectionView;


@end

@implementation QualityScanViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:Hide_TabBar_Notification object:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    status = 0;
    
    
    _imageArray = [[NSMutableArray alloc] initWithCapacity:3];
    _imageDataArray = [[NSMutableArray alloc] initWithCapacity:3];
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 22)];
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [rightBtn addTarget:self action:@selector(rightBtnAcion) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
    
    _mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
    //_mainScrollView.contentSize = CGSizeMake(kDeviceWidth, kDeviceHeight-NAV_HEIGHT+1.0f);
    _mainScrollView.backgroundColor = kWhiteColor;
    [self.view addSubview:_mainScrollView];
    
    [self initHeaderView];
    [self initCheckBox];
    [self initBottomView];
    /*
    UIControl *control00 = [[UIControl alloc] init];
    control00.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth/2.0f, 40.0f);
    [control00 addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    control00.tag = 110;
    [_mainScrollView addSubview:control00];
    
    UILabel *lbl00 = [[UILabel alloc] init];
    lbl00.frame = CGRectMake(10.0, (40-(Home_Small_Size+2.0f))/2.0f, 80.0f, Home_Small_Size+2.0f);
    lbl00.font = kAppFont(Home_Small_Size+2.0f);
    lbl00.textColor = kBlackColor;
    lbl00.text = @"公司名称";
    [control00 addSubview:lbl00];
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"06jiantou-"]];
    arrowImageView.frame = CGRectMake(kDeviceWidth/2.0f-10.0f/2.0f-10.0f,(40.0f-(19.0f/2.0f))/2.0f, 10.0f/2.0f, 19.0f/2.0f);
    [control00 addSubview:arrowImageView];
    
    UIView *lineView00 = [[UIView alloc] init];
    lineView00.frame = CGRectMake(0.0f, 40.0f-0.5f, kDeviceWidth/2.0f, 0.5f);
    lineView00.backgroundColor = kGreenColor;
    [control00 addSubview:lineView00];
    
    UIControl *control01 = [[UIControl alloc] init];
    control01.frame = CGRectMake(kDeviceWidth/2.0f, 0.0f, kDeviceWidth/2.0f, 40.0f);
    [control01 addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    control01.tag = 111;
    [_mainScrollView addSubview:control01];
    
    UILabel *lbl01 = [[UILabel alloc] init];
    lbl01.frame = CGRectMake(10.0, (40-(Home_Small_Size+2.0f))/2.0f, 80.0f, Home_Small_Size+2.0f);
    lbl01.font = kAppFont(Home_Small_Size+2.0f);
    lbl01.textColor = kBlackColor;
    lbl01.text = @"项目名称";
    [control01 addSubview:lbl01];
    
    UIImageView *arrowImageView01 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"06jiantou-"]];
    arrowImageView01.frame = CGRectMake(kDeviceWidth/2.0f-10.0f/2.0f-10.0f,(40.0f-(19.0f/2.0f))/2.0f, 10.0f/2.0f, 19.0f/2.0f);
    [control01 addSubview:arrowImageView01];
    
    UIView *lineView01 = [[UIView alloc] init];
    lineView01.frame = CGRectMake(0.0f, 40.0f-0.5f, kDeviceWidth/2.0f, 0.5f);
    lineView01.backgroundColor = kGreenColor;
    [control01 addSubview:lineView01];
    
    UIControl *control02 = [[UIControl alloc] init];
    control02.frame = CGRectMake(0.0f, control01.bottom, kDeviceWidth, 40.0f);
    [control02 addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    control02.tag = 112;
    [_mainScrollView addSubview:control02];
    
    UILabel *lbl02 = [[UILabel alloc] init];
    lbl02.frame = CGRectMake(10.0, (40-(Home_Small_Size+2.0f))/2.0f, 120.0f, Home_Small_Size+2.0f);
    lbl02.font = kAppFont(Home_Small_Size+2.0f);
    lbl02.textColor = kBlackColor;
    lbl02.text = @"关于施工工序";
    [control02 addSubview:lbl02];
    
    UIImageView *arrowImageView02 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"06jiantou-"]];
    arrowImageView02.frame = CGRectMake(kDeviceWidth-10.0f/2.0f-10.0f,(40.0f-(19.0f/2.0f))/2.0f, 10.0f/2.0f, 19.0f/2.0f);
    [control02 addSubview:arrowImageView02];
    
    UIView *lineView02 = [[UIView alloc] init];
    lineView02.frame = CGRectMake(0.0f, 40.0f-0.5f, kDeviceWidth, 0.5f);
    lineView02.backgroundColor = kGreenColor;
    [control02 addSubview:lineView02];
    
    UIView *statusView = [[UIView alloc] init];
    statusView.frame = CGRectMake(0.0f, control02.bottom, kDeviceWidth, 40.0f);
    [_mainScrollView addSubview:statusView];

    UILabel *statusLbl = [[UILabel alloc] init];
    statusLbl.frame = CGRectMake(10.0, (40-(Home_Small_Size+2.0f))/2.0f, 40.0f, Home_Small_Size+2.0f);
    statusLbl.font = kAppFont(Home_Small_Size+2.0f);
    statusLbl.textColor = kBlackColor;
    statusLbl.text = @"状态";
    [statusView addSubview:statusLbl];
    
    UIControl *button1 = [[UIControl alloc] init];
    button1.frame = CGRectMake(statusLbl.right+10.0f, 10.0f, 80+20+10.0f, 20.0f);
    [statusView addSubview:button1];
    
    _boxView = [[SSCheckBoxView alloc] initWithFrame:CGRectMake(statusLbl.right+10.0f, 10.0f, 80+20+10, 20.0f) style:kSSCheckBoxViewStyleMyStyle checked:YES];
    [_boxView setText:@"开始施工"];
    _currentBox = _boxView;
    _boxView.tag = 116;
    [_boxView setStateChangedTarget:self selector:@selector(boxViewAction:)];
    [statusView addSubview:_boxView];
    
    _boxView1 = [[SSCheckBoxView alloc] initWithFrame:CGRectMake(_boxView.right+10.0f, 10.0f, 80+20+10, 20.0f) style:kSSCheckBoxViewStyleMyStyle checked:NO];
    [_boxView1 setText:@"施工完成"];
    _boxView1.tag = 119;
    [_boxView1 setStateChangedTarget:self selector:@selector(boxViewAction:)];
    [statusView addSubview:_boxView1];
    
    UIView *lineView03 = [[UIView alloc] init];
    lineView03.frame = CGRectMake(0.0f, 40.0f-0.5f, kDeviceWidth, 0.5f);
    lineView03.backgroundColor = kGreenColor;
    [statusView addSubview:lineView03];
    
    UIView *inputView = [[UIView alloc] init];
    inputView.frame = CGRectMake(0.0f, statusView.bottom, kDeviceWidth, 100.0f);
    [_mainScrollView addSubview:inputView];
    
    UILabel *inputLbl = [[UILabel alloc] init];
    inputLbl.frame = CGRectMake(10.0, 20.0f, 40.0f, Home_Small_Size+2.0f);
    inputLbl.font = kAppFont(Home_Small_Size+2.0f);
    inputLbl.textColor = kBlackColor;
    inputLbl.text = @"描述";
    [inputView addSubview:inputLbl];
    
    UITextView *inputField = [[UITextView alloc] init];
    inputField.frame = CGRectMake(inputLbl.right, inputLbl.origin.y, kDeviceWidth-10*3-40.0f, 100-20-10);
    inputField.font = kAppFont(Home_Small_Size+2.0f);
    inputField.textColor = kGreenColor;
    [inputView addSubview:inputField];
    
    UIView *lineView04 = [[UIView alloc] init];
    lineView04.frame = CGRectMake(0.0f, inputView.height-0.5f, kDeviceWidth, 0.5f);
    lineView04.backgroundColor = kGreenColor;
    [inputView addSubview:lineView04];
    
    //UIImageView *addImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"add"]];
    //addImageView.frame = CGRectMake(10.0f, inputView.bottom + 10.0f, 40.0f, 40.0f);
    //[_mainScrollView addSubview:addImageView];
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setBackgroundImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [addBtn setBackgroundImage:[UIImage imageNamed:@"add"] forState:UIControlStateHighlighted];
    addBtn.frame = CGRectMake(10.0f, inputView.bottom + 10.0f, 100/2, 100/2);
    [addBtn addTarget:self action:@selector(addBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_mainScrollView addSubview:addBtn];
     */
}




- (void)initHeaderView{
    
    _headerView = [[UIView alloc] init];
    _headerView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, 40.0f+0.5f+40.0f);
    [_mainScrollView addSubview:_headerView];
    
    UIView *lineView0 = [[UIView alloc] init];
    lineView0.frame = CGRectMake(0.0f, 40.0f, kDeviceWidth, 0.5f);
    lineView0.backgroundColor = kGreenColor;
    [_headerView addSubview:lineView0];
    
    
    UIControl *lbl0Control = [[UIControl alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kDeviceWidth/2.0f, 40.0f)];
    lbl0Control.tag = 116;
    [lbl0Control addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:lbl0Control];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(kDeviceWidth/2.0f, 0.0f, 0.5f, 40.0f);
    lineView.backgroundColor = kGreenColor;
    [_headerView addSubview:lineView];
    
    UIControl *lbl1Control = [[UIControl alloc] initWithFrame:CGRectMake(kDeviceWidth/2.0f, 0.0f, kDeviceWidth/2.0f, 40.0f)];
    lbl1Control.tag = 118;
    [lbl1Control addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:lbl1Control];
    
    //ic_down@2x.png
    lbl0 = [[UILabel alloc] init];
    lbl0.frame = CGRectMake(0.0, (40-(Home_Small_Size+2.0f))/2.0f, kDeviceWidth/2.0f-26/2-2.0f, Home_Small_Size+2.0f);
    lbl0.font = kAppFont(Home_Small_Size+2.0f);
    lbl0.textColor = kBlackColor;
    
    
    if (companyDic != nil && companyDic.count > 0) {
        lbl0.text = [companyDic valueForKey:@"OrgName"];
    }else{
        lbl0.text = @"公司";
    }
    lbl0.textAlignment = NSTextAlignmentLeft;
    [lbl0Control addSubview:lbl0];
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_down"]];
    arrowImageView.frame = CGRectMake(kDeviceWidth/2.0f-26.0f/2.0f-2.0f, (40-11.0f/2.0f)/2.0f, 26/2, 11.0f/2.0f);
    [lbl0Control addSubview:arrowImageView];
    
    lbl1 = [[UILabel alloc] init];
    lbl1.frame = CGRectMake(0.0f, (40-(Home_Small_Size+2.0f))/2.0f, kDeviceWidth/2.0f-26/2-2.0f, Home_Small_Size+2.0f);
    lbl1.font = kAppFont(Home_Small_Size+2.0f);
    lbl1.textColor = kBlackColor;
    
    if (projectDic != nil && projectDic.count > 0) {
        lbl1.text = [projectDic valueForKey:@"ProjectName"];
    }else{
        lbl1.text = @"项目";
    }
    
    lbl1.textAlignment = NSTextAlignmentLeft;
    [lbl1Control addSubview:lbl1];
    
    UIImageView *arrowImageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_down"]];
    arrowImageView1.frame = CGRectMake(kDeviceWidth/2.0f-26.0f/2.0f-2.0f, (40-11.0f/2.0f)/2.0f, 26/2, 11.0f/2.0f);
    [lbl1Control addSubview:arrowImageView1];
    
    
    control02 = [[UIControl alloc] init];
    control02.frame = CGRectMake(0.0f, 40.0f, kDeviceWidth, 40.0f);
    [control02 addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    control02.tag = 112;
    [_mainScrollView addSubview:control02];
    
    lbl02 = [[UILabel alloc] init];
    lbl02.frame = CGRectMake(10.0, (40-(Home_Small_Size+2.0f))/2.0f, kDeviceWidth-20-10/2, Home_Small_Size+2.0f);
    lbl02.font = kAppFont(Home_Small_Size+2.0f);
    lbl02.textColor = kBlackColor;
    lbl02.text = @"所属关键工序";
    [control02 addSubview:lbl02];
    
    UIImageView *arrowImageView02 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"06jiantou-"]];
    arrowImageView02.frame = CGRectMake(kDeviceWidth-10.0f/2.0f-10.0f,(40.0f-(19.0f/2.0f))/2.0f, 10.0f/2.0f, 19.0f/2.0f);
    [control02 addSubview:arrowImageView02];
}


- (void)initCheckBox{
    _checkView = [[UIView alloc] init];
    _checkView.frame = CGRectMake(0.0f, _headerView.bottom, kDeviceWidth, 40.0f);
    [_mainScrollView addSubview:_checkView];
    
    UIView *lineView0 = [[UIView alloc] init];
    lineView0.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, 0.5f);
    lineView0.backgroundColor = kGreenColor;
    [_checkView addSubview:lineView0];
    
    UIView *lineView1 = [[UIView alloc] init];
    lineView1.frame = CGRectMake(0.0f, 39.5f, kDeviceWidth, 0.5f);
    lineView1.backgroundColor = kGreenColor;
    [_checkView addSubview:lineView1];
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.frame = CGRectMake(10.0f, (40-Home_Big_Size)/2.0f, 40.0f, Home_Big_Size);
    titleLbl.font = kAppFont(Home_Big_Size);
    titleLbl.text = @"状态";
    [_checkView addSubview:titleLbl];
    
    _boxView = [[SSCheckBoxView alloc] initWithFrame:CGRectMake(titleLbl.right, 10.0f, 80, 20.0f) style:kSSCheckBoxViewStyleMyStyle checked:YES];
    [_boxView setText:@"未开始"];
    _currentBox = _boxView;
    _boxView.tag = 116;
    [_boxView setStateChangedTarget:self selector:@selector(boxViewAction:)];
    [_checkView addSubview:_boxView];
    
    _boxView1 = [[SSCheckBoxView alloc] initWithFrame:CGRectMake(_boxView.right+10.0f, 10.0f, 80, 20.0f) style:kSSCheckBoxViewStyleMyStyle checked:NO];
    [_boxView1 setText:@"施工中"];
    _boxView1.tag = 118;
    [_boxView1 setStateChangedTarget:self selector:@selector(boxViewAction:)];
    [_checkView addSubview:_boxView1];
    
    _boxView2 = [[SSCheckBoxView alloc] initWithFrame:CGRectMake(_boxView1.right+10.0f, 10.0f, 80, 20.0f) style:kSSCheckBoxViewStyleMyStyle checked:NO];
    [_boxView2 setText:@"已竣工"];
    _boxView2.tag = 119;
    [_boxView2 setStateChangedTarget:self selector:@selector(boxViewAction:)];
    [_checkView addSubview:_boxView2];
}

- (void) initBottomView{
    
    _inputView = [[UIView alloc] init];
    _inputView.frame = CGRectMake(0.0f, _checkView.bottom, kDeviceWidth, 100.0f);
    [_mainScrollView addSubview:_inputView];
    
    UILabel *inputLbl = [[UILabel alloc] init];
    inputLbl.frame = CGRectMake(10.0, 20.0f, 40.0f, Home_Small_Size+2.0f);
    inputLbl.font = kAppFont(Home_Small_Size+2.0f);
    inputLbl.textColor = kBlackColor;
    inputLbl.text = @"描述";
    [_inputView addSubview:inputLbl];
    
    inputField = [[UITextField alloc] init];
    //inputField = [[UITextView alloc] init];
    inputField.frame = CGRectMake(inputLbl.right, inputLbl.origin.y, kDeviceWidth-10*3-40.0f, 100-20-10);
    inputField.font = kAppFont(Home_Small_Size+2.0f);
    inputField.textColor = kGreenColor;
    [_inputView addSubview:inputField];
    
    UIView *lineView04 = [[UIView alloc] init];
    lineView04.frame = CGRectMake(0.0f, _inputView.height-0.5f, kDeviceWidth, 0.5f);
    lineView04.backgroundColor = kGreenColor;
    [_inputView addSubview:lineView04];
    
    
//    addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [addBtn setBackgroundImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
//    [addBtn setBackgroundImage:[UIImage imageNamed:@"add"] forState:UIControlStateHighlighted];
//    addBtn.frame = CGRectMake(10.0f, _inputView.bottom + 10.0f, 100/2, 100/2);
//    [addBtn addTarget:self action:@selector(addBtnAction) forControlEvents:UIControlEventTouchUpInside];
//    [_mainScrollView addSubview:addBtn];
    
    [self configCollectionView];
}



- (void)controlAction:(UIControl*)sender
{
    switch (sender.tag) {
        case 116:
        {
            SelectCompanyAndProjectViewController *selectVC = [[SelectCompanyAndProjectViewController alloc] init];
            selectVC.delegate = self;
            [self.navigationController pushViewController:selectVC animated:YES];
        }
            break;
        case 118:
        {
            SelectCompanyAndProjectViewController *selectVC = [[SelectCompanyAndProjectViewController alloc] init];
            selectVC.delegate = self;
            [self.navigationController pushViewController:selectVC animated:YES];
        }
            break;
        case 119:
        {
            ShiGongStatusViewController *shiGongVC = [[ShiGongStatusViewController alloc] init];
            shiGongVC.delegate = self;
            [self.navigationController pushViewController:shiGongVC animated:YES];
        }
            break;
        case 112:
        {
            if (projectDic == nil || projectDic.count <= 0) {
                [[YMUIUtil getInstance] showTipViewAutoDis:@"请先选择公司和项目"];
                return;
            }

            
            SearchGuanJianGongXuViewController *searchVC = [[SearchGuanJianGongXuViewController alloc] initWithProject:projectDic status:status];
            searchVC.navTitle = @"关键工序搜索";
            searchVC.delegate = self;
            [self.navigationController pushViewController:searchVC animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)passData:(NSDictionary *)dic superDic:(NSDictionary *)superDic{
    projectDic = dic;
    companyDic = superDic;
    
    lbl0.text = [companyDic valueForKey:@"OrgName"];
    lbl1.text = [projectDic valueForKey:@"ProjectName"];
}


- (void)passDic:(NSDictionary *)dic{
    currentDic = dic;
    
    _myTableView = [[UITableView alloc] init];
    _myTableView.frame = CGRectMake(20.0f, _headerView.bottom, kDeviceWidth-20.0f, 30*4);
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.scrollEnabled = NO;
    [_mainScrollView addSubview:_myTableView];
    
    [_myTableView reloadData];
    
    _checkView.frame = CGRectMake(0.0f, _myTableView.bottom, kDeviceWidth, 40.0f);
    _inputView.frame = CGRectMake(0.0f, _checkView.bottom, kDeviceWidth, 100.0f);
    //addBtn.frame = CGRectMake(10.0f, _inputView.bottom + 10.0f, 100/2, 100/2);
    
    _collectionView.frame = CGRectMake(0, _inputView.bottom, self.view.tz_width, self.view.tz_height - 300);
    
    //lbl02.text = [NSString stringWithFormat:@"%@>%@>%@>%@", [dic valueForKey:@"Name"], [dic valueForKey:@"UnitProject"], [dic valueForKey:@"PartProject"], [dic valueForKey:@"SpecialProject"]];
}

- (void)rightBtnAcion
{
    if (currentDic == nil || currentDic.count <= 0) {
        [[YMUIUtil getInstance] showTipViewAutoDis:@"所属关键工序不能为空"];
        return;
    }
    
    NSMutableDictionary *tttttt = [[NSMutableDictionary alloc] initWithDictionary:currentDic];
    [tttttt setObject:[NSNumber numberWithInt:[[tttttt valueForKey:@"ID"] intValue]] forKey:@"RealID"];
    [tttttt setObject:inputField.text forKey:@"MiaoShu"];
    [tttttt setObject:_imageDataArray forKey:@"Image_List"];
    [tttttt setValue:[NSString stringWithFormat:@"%d", status] forKey:@"Status"];
    
    NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HHmmssSSS"];
    NSString *currentTime = [formatter stringFromDate:[NSDate date]];
    NSString *InfoIdStr = [NSString stringWithFormat:@"%@%@",@"9",currentTime];
    [tttttt setObject:InfoIdStr forKey:@"ID"]; //9位
    
    
    
    //GuanJianGongXu_Save
    NSArray *aaaa = [[NSUserDefaults standardUserDefaults] objectForKey:GuanJianGongXu_Save];
    
    if (aaaa != nil && aaaa.count > 0) {
        NSMutableArray *lllll = [[NSMutableArray alloc] initWithArray:aaaa];
        BOOL isExsit = NO;
        for (int i = 0; i < aaaa.count; i ++) {
            NSDictionary *ooo = [aaaa objectAtIndex:i];
            if ([[ooo valueForKey:@"ID"] floatValue] == [[tttttt valueForKey:@"ID"] floatValue]) {
                [lllll replaceObjectAtIndex:i withObject:tttttt];
                isExsit = YES;
            }
        }
        if (!isExsit) {
            [lllll addObject:tttttt];
        }
        [[NSUserDefaults standardUserDefaults] setObject:lllll forKey:GuanJianGongXu_Save];
    }else{
        NSMutableArray *yy = [[NSMutableArray alloc] initWithCapacity:3];
        [yy addObject:tttttt];
        [[NSUserDefaults standardUserDefaults] setObject:yy forKey:GuanJianGongXu_Save];
    }
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)boxViewAction2222:(SSCheckBoxView*)sender{
    
}

- (void)boxViewAction:(SSCheckBoxView*)sender
{
    if (sender == _currentBox)
    {
        return;
    }
    
    _currentBox.checked = NO;
    sender.checked = YES;
    _currentBox = sender;
    
    if (sender.tag == 116) {
        status = 0;
    }else if (sender.tag == 118){
        status = 1;
    }else{
        status = 2;
    }
}

- (void)addBtnAction
{

    UIActionSheet *chooseImageSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
    [chooseImageSheet showInView:self.view];

}






- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName

{
    //四列
    int totalloc = 4;
    CGFloat tempWidth = (kDeviceWidth-10.0f*5.0f)/4.0f;
    CGFloat tempHeight = (kDeviceWidth-10.0f*5.0f)/4.0f;
    
    int margin = 10;
    
    //UIImageView *ddd = [[UIImageView alloc] initWithImage:tempImage];
    //ddd.frame = CGRectMake(0.0f, 200, tempImage.size.width, tempImage.size.height);
    
    
    //[ddd setContentScaleFactor:[[UIScreen mainScreen] scale]];
    //ddd.contentMode =  UIViewContentModeScaleAspectFill;
    //ddd.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    //ddd.clipsToBounds  = YES;
    
    
    //ddd.backgroundColor = kRedColor;
    //[_mainScrollView addSubview:ddd];
    
    [_imageArray addObject:tempImage];
    NSData* imageData = UIImagePNGRepresentation(tempImage);
    [_imageDataArray addObject:imageData];
    
    for (int i = 0; i <= _imageArray.count; i ++) {
        
        int row=i/totalloc;//行号
        //1/3=0,2/3=0,3/3=1;
        int loc=i%totalloc;//列号
        
        CGFloat appviewx = margin+(margin+tempWidth)*loc;
        CGFloat appviewy = margin+(margin+tempHeight)*row;
        
        if (i == _imageArray.count) {
            addBtn.frame = CGRectMake(appviewx, _inputView.bottom+appviewy, tempWidth, tempHeight);
            return;
        }
        
        UIImageView *tempImageView = [[UIImageView alloc] init];
        UIImage *image = [_imageArray objectAtIndex:i];
        
        
        tempImageView.frame = CGRectMake(appviewx, _inputView.bottom+appviewy, tempWidth, tempHeight);
        tempImageView.image = image;
        tempImageView.contentMode = UIViewContentModeScaleToFill;
        //[tempImageView setClipsToBounds:YES];
        
        tempImageView.backgroundColor = [UIColor grayColor];
        [_mainScrollView addSubview:tempImageView];
        
        
    }
    
    /*
     NSLog(@"===TMP_UPLOAD_IMG_PATH===%@",TMP_UPLOAD_IMG_PATH);
     NSData* imageData = UIImagePNGRepresentation(tempImage);
     
     NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
     
     NSString* documentsDirectory = [paths objectAtIndex:0];
     
     // Now we get the full path to the file
     
     NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
     
     // and then we write it out
     TMP_UPLOAD_IMG_PATH=fullPathToFile;
     NSArray *nameAry=[TMP_UPLOAD_IMG_PATH componentsSeparatedByString:@"/"];
     NSLog(@"===new fullPathToFile===%@",fullPathToFile);
     NSLog(@"===new FileName===%@",[nameAry objectAtIndex:[nameAry count]-1]);
     
     [imageData writeToFile:fullPathToFile atomically:NO];
     
     [self onPostData];
     */
}





- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"first_cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.textColor = kRGB(205, 206, 207);
    cell.textLabel.font = kAppFont(Home_Small_Size+2);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"1.%@", [currentDic valueForKey:@"Name"]];
    }else if (indexPath.row ==1){
        cell.textLabel.text = [NSString stringWithFormat:@"2.%@", [currentDic valueForKey:@"SpecialProject"]];
    }else if (indexPath.row ==2){
        cell.textLabel.text = [NSString stringWithFormat:@"3.%@", [currentDic valueForKey:@"PartProject"]];
    }else{
        cell.textLabel.text = [NSString stringWithFormat:@"4.%@", [currentDic valueForKey:@"UnitProject"]];
    }
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30.0f;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



#pragma *************** select image *****************************
- (void)configCollectionView {
    _layout = [[LxGridViewFlowLayout alloc] init];
    _margin = 4;
    _itemWH = (self.view.tz_width - 3 * _margin - 4) / 4 - _margin;
    _layout.itemSize = CGSizeMake(_itemWH, _itemWH);
    _layout.minimumInteritemSpacing = _margin;
    _layout.minimumLineSpacing = _margin;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, _inputView.bottom, self.view.tz_width, self.view.tz_height - _inputView.bottom) collectionViewLayout:_layout];
    //CGFloat rgb = 244 / 255.0;
    _collectionView.alwaysBounceVertical = YES;
    _collectionView.backgroundColor = kWhiteColor;
    //_collectionView.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0];
    _collectionView.contentInset = UIEdgeInsetsMake(4, 4, 4, 4);
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_mainScrollView addSubview:_collectionView];
    [_collectionView registerClass:[TZTestCell class] forCellWithReuseIdentifier:@"TZTestCell"];
    
    
}


#pragma mark UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _selectedPhotos.count + 1;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TZTestCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZTestCell" forIndexPath:indexPath];
    cell.videoImageView.hidden = YES;
    if (indexPath.row == _selectedPhotos.count) {
        cell.imageView.image = [UIImage imageNamed:@"AlbumAddBtn.png"];
        cell.deleteBtn.hidden = YES;
    } else {
        cell.imageView.image = _selectedPhotos[indexPath.row];
        //cell.asset = _selectedAssets[indexPath.row];
        cell.deleteBtn.hidden = NO;
    }
    cell.deleteBtn.tag = indexPath.row;
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == _selectedPhotos.count) {
        BOOL showSheet = NO;//self.showSheetSwitch.isOn;
        if (showSheet) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"去相册选择", nil];
#pragma clang diagnostic pop
            [sheet showInView:self.view];
        } else {
            [self pushImagePickerController];
        }
    } else { // preview photos or video / 预览照片或者视频
        
        id asset;
        if (_selectedPhotos.count > _selectedAssets.count) {
            int tempInt = indexPath.row;
            int uuu = (_selectedPhotos.count-_selectedAssets.count);
            if ((tempInt - uuu) >= 0) {
                asset = _selectedAssets[(indexPath.row-(_selectedPhotos.count-_selectedAssets.count))];
            }
        }else{
            asset = _selectedAssets[indexPath.row];
        }
        //id asset = _selectedAssets[indexPath.row];
        BOOL isVideo = NO;
        if ([asset isKindOfClass:[PHAsset class]]) {
            PHAsset *phAsset = asset;
            isVideo = phAsset.mediaType == PHAssetMediaTypeVideo;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        } else if ([asset isKindOfClass:[ALAsset class]]) {
            ALAsset *alAsset = asset;
            isVideo = [[alAsset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo];
#pragma clang diagnostic pop
        }
        if (isVideo) { // perview video / 预览视频
            TZVideoPlayerController *vc = [[TZVideoPlayerController alloc] init];
            TZAssetModel *model = [TZAssetModel modelWithAsset:asset type:TZAssetModelMediaTypeVideo timeLength:@""];
            vc.model = model;
            [self presentViewController:vc animated:YES completion:nil];
        } else { // preview photos / 预览照片
            TZImagePickerController *imagePickerVc;
            if (_selectedPhotos.count > _selectedAssets.count) {
                int tempInt = indexPath.row;
                int uuu = (_selectedPhotos.count-_selectedAssets.count);
                if ((tempInt - uuu) >= 0) {
                    
                    imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos index:(indexPath.row-(_selectedPhotos.count-_selectedAssets.count))];
                    
                    
                    //TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos index:indexPath.row];
                    imagePickerVc.allowPickingOriginalPhoto = NO;//self.allowPickingOriginalPhotoSwitch.isOn;
                    imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
                    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
                        _selectedPhotos = [NSMutableArray arrayWithArray:photos];
                        _selectedAssets = [NSMutableArray arrayWithArray:assets];
                        _isSelectOriginalPhoto = isSelectOriginalPhoto;
                        _layout.itemCount = _selectedPhotos.count;
                        [_collectionView reloadData];
                        _collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
                    }];
                    [self presentViewController:imagePickerVc animated:YES completion:nil];
                    
                }
            }else{
                imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos index:indexPath.row];
                
                
                //TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithSelectedAssets:_selectedAssets selectedPhotos:_selectedPhotos index:indexPath.row];
                imagePickerVc.allowPickingOriginalPhoto = NO;//self.allowPickingOriginalPhotoSwitch.isOn;
                imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
                [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
                    _selectedPhotos = [NSMutableArray arrayWithArray:photos];
                    _selectedAssets = [NSMutableArray arrayWithArray:assets];
                    _isSelectOriginalPhoto = isSelectOriginalPhoto;
                    _layout.itemCount = _selectedPhotos.count;
                    [_collectionView reloadData];
                    _collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
                }];
                [self presentViewController:imagePickerVc animated:YES completion:nil];
            }
            
            
        }
    }
}

- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)sourceIndexPath didMoveToIndexPath:(NSIndexPath *)destinationIndexPath {
    if (sourceIndexPath.item >= _selectedPhotos.count || destinationIndexPath.item >= _selectedPhotos.count) return;
    UIImage *image = _selectedPhotos[sourceIndexPath.item];
    if (image) {
        [_selectedPhotos exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
        [_selectedAssets exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
        [_collectionView reloadData];
    }
}

#pragma mark - TZImagePickerController

- (void)pushImagePickerController {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:6 delegate:self];
    
#pragma mark - 四类个性化设置，这些参数都可以不传，此时会走默认设置
    imagePickerVc.isSelectOriginalPhoto = _isSelectOriginalPhoto;
    
    // 1.如果你需要将拍照按钮放在外面，不要传这个参数
    imagePickerVc.selectedAssets = _selectedAssets; // optional, 可选的
    imagePickerVc.allowTakePicture = NO;//self.showTakePhotoBtnSwitch.isOn; // 在内部显示拍照按钮
    
    // 2. Set the appearance
    // 2. 在这里设置imagePickerVc的外观
    // imagePickerVc.navigationBar.barTintColor = [UIColor greenColor];
    // imagePickerVc.oKButtonTitleColorDisabled = [UIColor lightGrayColor];
    // imagePickerVc.oKButtonTitleColorNormal = [UIColor greenColor];
    
    // 3. Set allow picking video & photo & originalPhoto or not
    // 3. 设置是否可以选择视频/图片/原图
    imagePickerVc.allowPickingVideo = NO;//self.allowPickingVideoSwitch.isOn;
    imagePickerVc.allowPickingImage = YES;//self.allowPickingImageSwitch.isOn;
    imagePickerVc.allowPickingOriginalPhoto = NO;//self.allowPickingOriginalPhotoSwitch.isOn;
    
    // 4. 照片排列按修改时间升序
    imagePickerVc.sortAscendingByModificationDate = NO;//self.sortAscendingSwitch.isOn;
#pragma mark - 到这里为止
    
    // You can get the photos by block, the same as by delegate.
    // 你可以通过block或者代理，来得到用户选择的照片.
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
    }];
    
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}

#pragma mark - UIImagePickerController

- (void)takePhoto {
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if ((authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) && iOS8Later) {
        // 无权限 做一个友好的提示
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"无法使用相机" message:@"请在iPhone的""设置-隐私-相机""中允许访问相机" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
        [alert show];
#define push @#clang diagnostic pop
    } else { // 调用相机
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
            self.imagePickerVc.sourceType = sourceType;
            if(iOS8Later) {
                _imagePickerVc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            }
            [self presentViewController:_imagePickerVc animated:YES completion:nil];
        } else {
            NSLog(@"模拟器中无法打开照相机,请在真机中使用");
        }
    }
}

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        TZImagePickerController *tzImagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:6 delegate:self];
        tzImagePickerVc.sortAscendingByModificationDate = NO;//self.sortAscendingSwitch.isOn;
        [tzImagePickerVc showProgressHUD];
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        // save photo and get asset / 保存图片，获取到asset
        [[TZImageManager manager] savePhotoWithImage:image completion:^{
            [[TZImageManager manager] getCameraRollAlbum:NO allowPickingImage:YES completion:^(TZAlbumModel *model) {
                [[TZImageManager manager] getAssetsFromFetchResult:model.result allowPickingVideo:NO allowPickingImage:YES completion:^(NSArray<TZAssetModel *> *models) {
                    [tzImagePickerVc hideProgressHUD];
                    TZAssetModel *assetModel = [models firstObject];
                    if (tzImagePickerVc.sortAscendingByModificationDate) {
                        assetModel = [models lastObject];
                    }
                    [_selectedAssets addObject:assetModel.asset];
                    [_selectedPhotos addObject:image];
                    [_collectionView reloadData];
                }];
            }];
        }];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIActionSheetDelegate

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
#pragma clang diagnostic pop
    if (buttonIndex == 0) { // take photo / 去拍照
        [self takePhoto];
    } else if (buttonIndex == 1) {
        [self pushImagePickerController];
    }
}

#pragma mark - UIAlertViewDelegate

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
#pragma clang diagnostic pop
    if (buttonIndex == 1) { // 去设置界面，开启相机访问权限
        if (iOS8Later) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        } else {
            // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=Photos"]];
        }
    }
}

#pragma mark - TZImagePickerControllerDelegate
// ++++++++ pxb ++++++++++++++
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    
    if (_selectedPhotos.count <= 0) {
        _selectedPhotos = [NSMutableArray arrayWithArray:photos];
        
        _imageArray = _selectedPhotos;
        
    }else{
        
        [_imageArray removeAllObjects];
        _imageDataArray = [[NSMutableArray alloc] initWithArray:[currentDic objectForKey:@"Image_List"]];
        for (NSData *eee in _imageDataArray) {
            UIImage *image = [UIImage imageWithData: eee];
            [_imageArray addObject:image];
        }
        
        NSMutableArray *ddddd = [[NSMutableArray alloc] initWithArray:_imageArray];
        
        for (UIImage *imageNNN in photos) {
            [ddddd addObject:imageNNN];
        }
        
        _imageArray = ddddd;
        
        _selectedPhotos = _imageArray;
    }
    
    
    
    
    [_imageDataArray removeAllObjects];
    for (UIImage *dddd in _imageArray) {
        NSData* imageData = UIImageJPEGRepresentation(dddd,1.0);
        [_imageDataArray addObject:imageData];
    }
    
    _selectedAssets = [NSMutableArray arrayWithArray:assets];
    _isSelectOriginalPhoto = isSelectOriginalPhoto;
    _layout.itemCount = _selectedPhotos.count;
    
    _collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 4 +2) * (_margin + _itemWH));
    
    [_collectionView reloadData];
    
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingVideo:(UIImage *)coverImage sourceAssets:(id)asset {
    _selectedPhotos = [NSMutableArray arrayWithArray:@[coverImage]];
    _selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
    _layout.itemCount = _selectedPhotos.count;
    [_collectionView reloadData];
}


#pragma mark Click Event
- (void)deleteBtnClik:(UIButton *)sender {
    if (_selectedPhotos.count > _selectedAssets.count) {
        int kkk = sender.tag;
        int mmm = (_selectedPhotos.count-_selectedAssets.count);
        if ((kkk - mmm) >= 0) {
            [_selectedAssets removeObjectAtIndex:(sender.tag-(_selectedPhotos.count-_selectedAssets.count))];
        }
    }else{
        [_selectedAssets removeObjectAtIndex:sender.tag];
    }
    [_selectedPhotos removeObjectAtIndex:sender.tag];
    //[_selectedAssets removeObjectAtIndex:sender.tag];
    _layout.itemCount = _selectedPhotos.count;
    
    
    //[_imageArray removeAllObjects];
    [_imageDataArray removeAllObjects];
    
    _imageArray = _selectedPhotos;
    
    for (UIImage *dddd in _imageArray) {
        NSData* imageData = UIImageJPEGRepresentation(dddd,1.0);
        [_imageDataArray addObject:imageData];
    }
    
    
    [_collectionView performBatchUpdates:^{
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.tag inSection:0];
        [_collectionView deleteItemsAtIndexPaths:@[indexPath]];
    } completion:^(BOOL finished) {
        [_collectionView reloadData];
    }];
}


@end
